
//
//  WorkerDetailViewController.m
//  MagictoHire
//
//  Created by 锋 on 2017/12/22.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "WorkerDetailViewController.h"
#import "JobDetailMessageTableViewCell.h"
#import "JobProducePointTableViewCell.h"
#import "EvaluateProducePointTableViewCell.h"
#import "RecommendJobDetailTableViewCell.h"
#import "RecommendPointTableViewCell.h"
#import "JobDetailModel.h"
#import "SpotsModel.h"
#import "WorkModel.h"
#import "EvaluateSpotsModel.h"
#import "ProduceMachinesModel.h"

@interface WorkerDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *workDetailTableView;
@property (nonatomic, strong) UILabel *jobKindLabel;
@property (nonatomic, strong) NSArray *jobDetailArr;
@property (nonatomic, assign) BOOL isSetUp;
@property (nonatomic, assign) BOOL isSetUpWorker;
@end

@implementation WorkerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isSetUp = NO;
    _isSetUpWorker = NO;
    [self setupUI];
    [self setupWorkerDetailTableView];
    [self requestWorkerDetailData];
    // Do any additional setup after loading the view.
}

- (void)setupUI {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"项目详情";
    self.view.backgroundColor = BacksColor;
}
- (void)setupWorkerDetailTableView {
    _workDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) style:UITableViewStyleGrouped];
    _workDetailTableView.delegate = self;
    _workDetailTableView.dataSource = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
    view.backgroundColor = [UIColor whiteColor];
    _workDetailTableView.tableHeaderView = view;
    _workDetailTableView.tableFooterView = view;
    _workDetailTableView.backgroundColor = BacksColor;
    if (@available(iOS 11.0, *)) {
        _workDetailTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _workDetailTableView.estimatedRowHeight = 0;
    _workDetailTableView.estimatedSectionHeaderHeight = 0;
    _workDetailTableView.estimatedSectionFooterHeight = 0;
    [self.view addSubview:_workDetailTableView];
}

- (void)requestWorkerDetailData {
    WEAKSELF;
//    NSDictionary *dic;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestProjectDetailParmers:@{@"id" : _ID} Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             _jobDetailArr = [JobDetailModel mj_objectArrayWithKeyValuesArray:@[obj[@"data"]]];
             
             WEAKSELF;
             dispatch_async(dispatch_get_main_queue(), ^{
                 [weakSelf.workDetailTableView reloadData];
             });
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

// MARK:tableview的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return 2;
    } else if (section == 2) {
        JobDetailModel *detailModel = _jobDetailArr[0];
        return detailModel.spots.count + 2;
    } else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    JobDetailModel *detailModel = _jobDetailArr[0];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 50;
        } else {
            return 78;
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return 50;
        } else {
            return 110 + detailModel.machines.count * 20 + 30 + 20 + detailModel.workGroups.count * 20 + 30;
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            return 78;
        } else {
            return 44;
        }
    } else {
        return 44;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section != 0) {
        return 5;
    } else {
        return 0.000001;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JobDetailModel *detailModel = _jobDetailArr[0];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            NSString *ID = @"cellID";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
            }
            if (!_jobKindLabel) {
                _jobKindLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 0, 130, 50)];
                _jobKindLabel.textColor = UIColorHexValue(0x8194C4);
                _jobKindLabel.font = [UIFont systemFontOfSize:16];
                [cell.contentView addSubview:_jobKindLabel];
                UILabel *datelabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 150 - 12, 0, 150, 50)];
                datelabel.tag = 600 + indexPath.row;
                datelabel.textAlignment = NSTextAlignmentRight;
                datelabel.textColor = UIColorHexValue(0x999999);
                datelabel.font = [UIFont systemFontOfSize:12];
                [cell.contentView addSubview:datelabel];
            }
            _jobKindLabel.text = detailModel.name;
            UILabel *datelabel = [cell.contentView viewWithTag:600 + indexPath.row];
            datelabel.text = [NSString stringWithFormat:@"%@-%@", detailModel.startDate, detailModel.endDate];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        } else {
            JobDetailMessageTableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:@"JobDetailMessageTableViewCell" owner:self options:nil].lastObject;
            cell.nameLabel.text = detailModel.employerName;
            cell.kindLabel.text = detailModel.productCategoryName;
            cell.moneyLabel.text = detailModel.budget;
            cell.evaluateBtn.hidden = YES;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            NSString *ID = @"cellID1";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
            }
            if (![cell.contentView viewWithTag:100 + indexPath.row]) {
                UILabel *positionlabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 0, 120, 50)];
                positionlabel.textColor = UIColorHexValue(0x4D4D4D);
                positionlabel.tag = 100 + indexPath.row;
                positionlabel.font = [UIFont systemFontOfSize:15];
                positionlabel.text = @"岗位描述";
                [cell.contentView addSubview:positionlabel];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        } else {
            NSString *ID = @"cellID2";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
            }
            if (![cell.contentView viewWithTag:200 + indexPath.row]) {
                UILabel *produceDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 20, 120, 20)];
                produceDetailLabel.textColor = UIColorHexValue(0x4D4D4D);
                produceDetailLabel.tag = 200 + indexPath.row;
                produceDetailLabel.text = @"生产说明:";
                produceDetailLabel.font = [UIFont systemFontOfSize:13];
                [cell.contentView addSubview:produceDetailLabel];
            }
            if (![cell.contentView viewWithTag:300 + indexPath.row]) {
                UILabel *produceLabel = [[UILabel alloc] init];
                produceLabel.textColor = UIColorHexValue(0x999999);
                produceLabel.tag = 300 + indexPath.row;
                produceLabel.font = [UIFont systemFontOfSize:13];
                CGRect textViewHeight = [Utilities calculateWidthAndHeightWithWidth:SCREEN_WIDTH - 28 height:1000 text:produceLabel.text font:[UIFont systemFontOfSize:13]];
                CGFloat aHeight = textViewHeight.size.height - 20 > 0 ? textViewHeight.size.height - 20 : 0;
                CGFloat height = 20 + aHeight;
                if (height > 38) {
                    height = 38;
                }
                produceLabel.frame = CGRectMake(14, 40, SCREEN_WIDTH - 28, height);
                [cell.contentView addSubview:produceLabel];
            }
            
            if (![cell.contentView viewWithTag:700 + indexPath.row]) {
                UILabel *produceMachineLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 90, 120, 20)];
                produceMachineLabel.textColor = UIColorHexValue(0x4D4D4D);
                produceMachineLabel.tag = 700 + indexPath.row;
                produceMachineLabel.text = @"生产设备:";
                produceMachineLabel.font = [UIFont systemFontOfSize:13];
                [cell.contentView addSubview:produceMachineLabel];
            }
            if (!_isSetUp) {
                for (int i = 0; i < detailModel.machines.count; i++) {
                    _isSetUp = YES;
                    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 110 + i * 20, (SCREEN_WIDTH - 28) / 2, 20)];
                    leftLabel.tag = 1000 + i;
                    leftLabel.textAlignment = NSTextAlignmentLeft;
                    leftLabel.textColor = UIColorHexValue(0x999999);
                    leftLabel.font = [UIFont systemFontOfSize:13];
                    [cell.contentView addSubview:leftLabel];
                    UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(14 + (SCREEN_WIDTH - 28) / 2, 110 + i * 20, (SCREEN_WIDTH - 28) / 2, 20)];
                    rightLabel.tag = 2000 + i;
                    rightLabel.textAlignment = NSTextAlignmentRight;
                    rightLabel.textColor = UIColorHexValue(0x999999);
                    rightLabel.font = [UIFont systemFontOfSize:13];
                    [cell.contentView addSubview:rightLabel];
                }
            }
            for (int i = 0; i < detailModel.machines.count; i++) {
                ProduceMachinesModel *machines = detailModel.machines[i];
                UILabel *leftLabel = [cell.contentView viewWithTag:1000 + i];
                UILabel *rightLabel = [cell.contentView viewWithTag:2000 + i];
                leftLabel.text = machines.machine.name;
                rightLabel.text = [NSString stringWithFormat:@"%@台", machines.count];
            }
            
            if (![cell.contentView viewWithTag:800 + indexPath.row]) {
                UILabel *workerLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 110 + detailModel.machines.count * 20 + 30, 120, 20)];
                workerLabel.textColor = UIColorHexValue(0x4D4D4D);
                workerLabel.tag = 800 + indexPath.row;
                workerLabel.text = @"所需工人:";
                workerLabel.font = [UIFont systemFontOfSize:13];
                [cell.contentView addSubview:workerLabel];
            }
            UILabel *workerLabel = [cell.contentView viewWithTag:800 + indexPath.row];
            workerLabel.frame = CGRectMake(14, 110 + detailModel.machines.count * 20 + 30, 120, 20);
            if (!_isSetUpWorker) {
                for (int i = 0; i < detailModel.workGroups.count; i++) {
                    _isSetUpWorker = YES;
                    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 110 + i * 20 + detailModel.machines.count * 20 + 30 + 20, (SCREEN_WIDTH - 28) / 2, 20)];
                    leftLabel.tag = 3000 + i;
                    leftLabel.textAlignment = NSTextAlignmentLeft;
                    leftLabel.textColor = UIColorHexValue(0x999999);
                    leftLabel.font = [UIFont systemFontOfSize:13];
                    [cell.contentView addSubview:leftLabel];
                    UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(14 + (SCREEN_WIDTH - 28) / 2, 110 + i * 20 + detailModel.machines.count * 20 + 30 + 20, (SCREEN_WIDTH - 28) / 2, 20)];
                    rightLabel.tag = 4000 + i;
                    rightLabel.textAlignment = NSTextAlignmentRight;
                    rightLabel.textColor = UIColorHexValue(0x999999);
                    rightLabel.font = [UIFont systemFontOfSize:13];
                    [cell.contentView addSubview:rightLabel];
                }
            }
            for (int i = 0; i < detailModel.workGroups.count; i++) {
                WorkModel *model = detailModel.workGroups[i];
                UILabel *leftLabel = [cell.contentView viewWithTag:3000 + i];
                UILabel *rightLabel = [cell.contentView viewWithTag:4000 + i];
                leftLabel.text = model.workName;
                rightLabel.text = [NSString stringWithFormat:@"%@人", model.count];
            }
            
            UILabel *produceLabel = [cell.contentView viewWithTag:300 + indexPath.row];
            produceLabel.text = detailModel.remark;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            RecommendJobDetailTableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:@"RecommendJobDetailTableViewCell" owner:self options:nil].lastObject;
            cell.nameLabel.text = detailModel.factoryName;
            cell.addressLabel.text = detailModel.factoryAddress;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        } else if (indexPath.row == 1) {
            NSString *ID = @"cellID3";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
            }
            if (![cell.contentView viewWithTag:400 + indexPath.row]) {
                UILabel *producePointLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 0, 120, 44)];
                producePointLabel.textColor = UIColorHexValue(0x4D4D4D);
                producePointLabel.text = @"生产节点";
                producePointLabel.tag = 400 + indexPath.row;
                producePointLabel.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:producePointLabel];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        } else {
            JobProducePointTableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:@"JobProducePointTableViewCell" owner:self options:nil].lastObject;
            if (indexPath.row == 2) {
                [cell.signBtn setImage:[UIImage imageNamed:@"guanlizhe"] forState:UIControlStateNormal];
            } else {
                [cell.signBtn setTitle:[NSString stringWithFormat:@"%ld", indexPath.row - 2] forState:UIControlStateNormal];
            }
            if (detailModel.spots.count > 0) {
                SpotsModel *model = detailModel.spots[indexPath.row - 2];
                cell.nameLabel.text = model.workerName;
                cell.produceNameLabel.text = model.workName;
                if (model.wage && ![model.wage isKindOfClass:[NSNull class]] && model.wage.length>0) {
                    cell.priceLabel.text = [NSString stringWithFormat:@"%@元/小时", model.wage];
                } else {
                    cell.priceLabel.text = [NSString stringWithFormat:@"%@元/小时", model.forecastWage];
                }
                [cell.timeBtn setTitle:[NSString stringWithFormat:@"%@小时", model.hours] forState:UIControlStateNormal];
            }
            if (indexPath.row == detailModel.spots.count) {
                cell.lineLabel.hidden = YES;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            return cell;
        }
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
